package com.dfiov.cloud.msmtools.utils;


import com.dfiov.cloud.msmtools.contant.GatewayConstant;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pateo.qingcloud.base.auth.common.session.AuthSession;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
 * @program: mswmbluetoothkey
 * @description:
 * @author: lvyuan
 * @create: 2022-01-08 13:21
 */
@Component
public class GatewayUtil {
    private static final Logger log = LoggerFactory.getLogger(GatewayUtil.class);

    private GatewayUtil() {
    }

    public static AuthSession getAuthSession(HttpServletRequest request) {
        AuthSession as = null;
        String sessionJsonStr = request.getHeader(GatewayConstant.HEADER_SESSION);
        if (sessionJsonStr != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

            try {
                as = (AuthSession)objectMapper.readValue(sessionJsonStr, AuthSession.class);
            } catch (IOException var5) {
                log.warn(var5.getMessage(), var5);
            }
        }

        return as;
    }

    public static String getProjectId(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEADER_PROJECT);
    }

    public static String getAuthSessionToJson(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEADER_SESSION);
    }

    public static String getOtherProjectId(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEAD_USER_OTHER_PROJECTID) == null ? "" : request.getHeader(GatewayConstant.HEAD_USER_OTHER_PROJECTID);
    }

    public static String getOrginProjectId(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEAD_ORIGIN_PROJECTID) == null ? "" : request.getHeader(GatewayConstant.HEAD_ORIGIN_PROJECTID);
    }

    public static String getSerialNumber(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEAD_SERIAL_NUMBER) == null ? "" : request.getHeader(GatewayConstant.HEAD_SERIAL_NUMBER);
    }

    public static int getProjectDat(HttpServletRequest request) {
        int deviceAuthType = 1;
        String deviceAuthTypeHead = request.getHeader(GatewayConstant.HEADER_PROJECT_DEVICEAUTHTYPE);
        if (deviceAuthTypeHead != null) {
            deviceAuthType = Integer.parseInt(deviceAuthTypeHead);
        }

        return deviceAuthType;
    }

    public static String getProjectKey(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEADER_PROJECT_KEY);
    }

    public static int getProjectAccountAuthType(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEADER_ACCOUNT_AUTH_TYPE) == null ? 0 : Integer.parseInt(request.getHeader(GatewayConstant.HEADER_ACCOUNT_AUTH_TYPE));
    }

    public static int getProjectAccountRoleAuthType(HttpServletRequest request) {
        return request.getHeader(GatewayConstant.HEADER_ACCOUNT_ROLE_AUTH_TYPE) == null ? 1 : Integer.parseInt(request.getHeader(GatewayConstant.HEADER_ACCOUNT_ROLE_AUTH_TYPE));
    }

    public static String getRequestHeadParam(HttpServletRequest request, String requestHeadKey) {
        return request.getHeader(requestHeadKey);
    }

    protected static AuthSession getAuthSession(HttpServletRequest request, Long userAccountSid, String aid) {
        if (request != null) {
            log.debug("AuthSession Header " + GatewayConstant.HEADER_SESSION + " = {}",
                    request.getHeader(GatewayConstant.HEADER_SESSION));
        } else {
            log.debug("request is null");
        }

        AuthSession authSession = GatewayUtil.getAuthSession(request);

        if (Objects.isNull(authSession) && userAccountSid != null && StringUtils.isNotBlank(aid)) {
            authSession = new AuthSession();
            authSession.setUid(userAccountSid);
            authSession.setAid(aid);
        }

        return authSession;
    }
}

